package com.jialanmo.zhihudaily.ui.main;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.jialanmo.helper.recyclerview.HeaderAndFooterAdapter;
import com.jialanmo.helper.OnItemClickListener;
import com.jialanmo.helper.recyclerview.OnLoadMoreListener;
import com.jialanmo.helper.recyclerview.OnViewCreateListener;
import com.jialanmo.helper.viewpager.SlidePagerAdapter;
import com.jialanmo.zhihudaily.R;
import com.jialanmo.zhihudaily.data.model.Daily;
import com.jialanmo.zhihudaily.data.model.News;
import com.jialanmo.zhihudaily.data.model.Story;
import com.jialanmo.zhihudaily.data.model.TopStory;
import com.jialanmo.zhihudaily.ui.news.NewsActicity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import butterknife.BindView;
import butterknife.ButterKnife;

public final class MainFragment extends Fragment implements MainContract.FragmentView {

    private final MainContract.FragmentPresenter mPresenter = new MainFragmentPresenter(this);
    private SlidePagerAdapter<TopStory> mTopStoryPagerAdapter;
    private final HeaderAndFooterAdapter<Object> mStoryAdapter = new HeaderAndFooterAdapter<>(new StoryAdapter(new OnItemClickListener() {
        @Override
        public void onItemClick(int position) {
            Object data = mStoryAdapter.getData().get(position);
            if (data instanceof Story) {
                Intent intent = new Intent(getContext(), NewsActicity.class);
                intent.putExtra(News.NEWS_ID, ((Story) data).getId());
                startActivity(intent);
            }
        }
    }));

    private int mCurrent;

    private View mView;
    @BindView(R.id.fragment_main_swiperefreshlayout)
    SwipeRefreshLayout mSwipeRefreshLayout;
    @BindView(R.id.fragment_main_recyclerview_latest)
    RecyclerView mRecyclerView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (mView == null) {
            mView = inflater.inflate(R.layout.fragment_main, container, false);
            ButterKnife.bind(this, mView);

            mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
            mStoryAdapter.setHeaderView(new OnViewCreateListener() {
                @Override
                public View onViewCreate(ViewGroup parent) {
                    mTopStoryPagerAdapter = new SlidePagerAdapter<>(parent, new TopStoryPagerAdapter(new OnItemClickListener() {
                        @Override
                        public void onItemClick(int position) {
                            Intent intent = new Intent(getContext(), NewsActicity.class);
                            intent.putExtra(News.NEWS_ID, mTopStoryPagerAdapter.getData().get(position).getId());
                            startActivity(intent);
                        }
                    }));
                    return mTopStoryPagerAdapter.getRootView();
                }
            });
            mStoryAdapter.setOnLoadMoreListener(new OnLoadMoreListener() {
                @Override
                public void onLoadMore() {
                    mPresenter.getBefore(mCurrent);
                }
            }, 0);
            mRecyclerView.setHasFixedSize(true);
            mRecyclerView.setAdapter(mStoryAdapter);

            mSwipeRefreshLayout.setProgressViewOffset(false, 0, (int) (64 * getContext().getResources().getDisplayMetrics().density));
            mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    mPresenter.getLatest();
                }
            });

            mPresenter.start();
        }
        return mView;
    }

    @Override
    public void showLatest(Daily daily) {
        mCurrent = 0;
        mTopStoryPagerAdapter.resetData(daily.getTopStories());
        List<Object> objects = new ArrayList<>(daily.getStories().size());
        objects.addAll(daily.getStories());
        mStoryAdapter.getData().clear();
        mStoryAdapter.getData().addAll(objects);
        mStoryAdapter.notifyDataSetChanged();
    }

    @Override
    public void showBefore(Daily daily) {
        mCurrent++;
        List<Object> objects = new ArrayList<>(daily.getStories().size() + 1);
        objects.add(new SimpleDateFormat("MM月dd日 EEEE", Locale.CHINESE).format(daily.getDate()));
        objects.addAll(daily.getStories());
        int index = mStoryAdapter.getItemCount();
        mStoryAdapter.getData().addAll(objects);
        mStoryAdapter.notifyItemRangeInserted(index, objects.size());
    }

    @Override
    public void setRefreshing(boolean refreshing) {
        mSwipeRefreshLayout.setRefreshing(refreshing);
    }

    @Override
    public void showMessage(String msg) {
        Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT).show();
    }
}
